import base64
from mistralai import Mistral

def encode_pdf(file_path):
    try:
        with open(file_path, 'rb') as f:
            return base64.b64encode(f.read()).decode('utf-8')
    except FileNotFoundError:
        print(f"Error: The file {file_path} was not found.")
        return None
    except Exception as e:  # Added general exception handling
        print(f"Error: {e}")
        return None

api_key = "bZYUgHCt7DvguXuWP546vAA0d0fadLlA"

def call_ocr_pdf(base64Data):
    if base64Data is None:
        print("No base64 data provided.")
        return None
    client = Mistral(api_key = api_key)
    ocr_response = client.ocr.process(
        model="mistral-ocr-latest",
        document={
            "type": "document_url",
            "document_url": f"data:application/pdf;base64,{base64Data}"
        },
        include_image_base64=True
    )
    print("OCR Response:", ocr_response.pages)
    return ocr_response

import os
if __name__ == "__main__":
    home_dir = os.path.expanduser("~")
    pdf_path = os.path.join(home_dir, "Downloads", "文档导入-测试输入", "SOC文档.pdf")
    pdf_path = r"C:\Users\18210\Downloads\文档导入-测试输入\SOC文档.pdf"
    docx_path = os.path.join(home_dir, "Downloads", "文档导入-测试输入", "DSP文档.docx")
    docx_path = r"C:\Users\18210\Downloads\文档导入-测试输入\SOC文档.docx"
    base64Data = encode_pdf(docx_path)
    responses = call_ocr_pdf(base64Data)